﻿<?php

class AdminController extends Controller
{
	
	public function actionIndex()
	{
		$this->render('index');
	}


	public function actionWriteBlog()
	{
		if(Yii::app()->request->isAjaxRequest){
			$content = $_POST['blog'];
			$imageUrl = $_POST['imgUrl'];
			$sortId = $_POST['sortId'];
			$title = $_POST['title'];
			$brief = substr(rtrim(strip_tags($content)),0,50);

			$article_list = new ArticleList;
			$article_list->article_title = $title;
			$article_list->article_brief = $brief;
			$article_list->article_createtime = date('Y-m-d');
			$article_list->article_image_url = $imageUrl;
			$article_list->article_content = $content;
			$article_list->article_sort_id = $sortId;
			$article_list->save();
		}
		$this->render('WriteBlog');
	}

	public function actionCreateSort()
	{
		if(Yii::app()->request->isAjaxRequest){
			$sort = $_POST['articleSort'];
			$article_sort = new articleSort;
			$article_sort->sort_name = $sort;
			$article_sort->sort_createtime = date('Y-m-d');
			$article_sort->save();
			echo true;
		}
	}

	public function actionSelectTag()
	{
		if(Yii::app()->request->isAjaxRequest)
		{
			//$selectTag = new article_sort;
			$Tag=ArticleSort::model()->findAll(array(
				'select'=>'article_sort_id,sort_name',
				//'condition'=>'1 limit 3',
			));
			echo CJSON::encode($Tag);
		}
	}

	public function actionManage()
    {

        if(isset($_GET['sign']))
        {
        	$sign = $_GET['sign'];
        	switch ($sign) {
        		case 'autited':
        			$sql_param = '1';
        			break;
        		case 'unAutited':
        			$sql_param = '0';
        			break;
        		case 'tursh':
        			$sql_param = '3';
        			break;
        		default:
        			$sql_param = '0';
        			break;
        	}
        }else{
        	$sql_param = '1';
        }
       		$paramsArray = $this->getPageArray($sql_param,'1');
	        $this->render('manage',$paramsArray);
        
        }

    
    public function getPageArray($whereParams,$currentPage)
    {
    	$connection = Yii::app()->db;
    	$queryCount = 'select * from article_list where article_list.article_status ='.$whereParams;
        $count = $connection->createCommand($queryCount);
        $sumNumber = count($count->queryColumn()); 
        $limitNumber = 20;
        $sql = 'select article_list.* , article_sort.sort_name from article_list left join article_sort on article_list.article_sort_id=article_sort.article_sort_id  where article_list.article_status ='.$whereParams.' limit '.$limitNumber*($currentPage-1).' , '.$limitNumber; 
        $command =  $connection->createCommand($sql);
        $article =  $command->query()->readAll();
        $url = 'AjaxPage';
    	$params = array('whereParams'=>$whereParams);
    	return array('articleList'=>$article,
        							'currentPage'=>$currentPage,
        							'sumNumber'=>$sumNumber,
        							'limitNumber'=>$limitNumber,
        							'url'=>$url,
        							'showNumber'=>'10',
        							'params'=>$params,
        						);

    }

    public function actionAjaxPage()
    {
    	if(isset($_POST['params']))
        {
        	//$params = json_decode($_POST['params'],true);
        	$params = $_POST['params'];
        	//print_r($params);
        	//echo $params;
       	foreach ($params as $key => $value) 
        	{
        		# code...
        		$$key = $value ;
        	}
        }else{
        	$currentPage = 1;
        }

  		$paramsArray = $this->getPageArray($whereParams,$currentPage);
		$str = '';
    	$articleList = $paramsArray['articleList'];
    	$limitNumber = $paramsArray['limitNumber'];
    	$sumNumber = $paramsArray['sumNumber'];
    	$currentPage = $paramsArray['currentPage'];
    	$url = $paramsArray['url'];
    	$showNumber = $paramsArray['showNumber'];
    	$params = $paramsArray['params'];
    	foreach ($articleList as $key => $value) 
		{ 
			$str .= '<tr style="border-bottom-color:#000000">'.
				'<td>'. ($key+1).'</td>'.
				'<td class="name">'.$articleList[$key]['article_title'].'</td>'.
				'<td>'.$articleList[$key]['browse_number'].'</td>'.
				'<td>'.$articleList[$key]['comment_number'].'</td>'.
				'<td>'.$articleList[$key]['sort_name'].'</td>'.
				'<td>'.$articleList[$key]['article_createtime'].'</td>'.
				'<td><input type="button" name="deleteList" value="删除"></td>'.
			'</tr>';
		}
		$strPageObj = new ajaxPage($limitNumber,$sumNumber,$currentPage,$url ,$showNumber,$params ); 
		$strPage =$strPageObj->showNumberPage();
		$data =  array(
			'tableStr'=>$str,
			'pageStr'=>$strPage,
			);
		echo CJSON::encode($data);

    }



    



	// Uncomment the following methods and override them if needed
	/*
	public function filters()
	{
		// return the filter configuration for this controller, e.g.:
		return array(
			'inlineFilterName',
			array(
				'class'=>'path.to.FilterClass',
				'propertyName'=>'propertyValue',
			),
		);
	}

	public function actions()
	{
		// return external action classes, e.g.:
		return array(
			'action1'=>'path.to.ActionClass',
			'action2'=>array(
				'class'=>'path.to.AnotherActionClass',
				'propertyName'=>'propertyValue',
			),
		);
	}
	*/
}


